'use strict';
/**
 * Created by freezesoul on 2015-10-13.
 */
define([
        'angular',
        'app/modules/dictionary/define',
        'app/modules/dictionary/services/dictionary'
    ],
    function(angular) {
        angular.module('ad.dictionary')
            .controller('adDictionaryEditController',['$scope','$uibModalInstance','adDictionaryService','item',function($scope,$uibModalInstance,service,item) {
                var defaultType = "默认类型";

                $scope.item = item;
                $scope.dictionaryTypes = [{
                    dictionary_code: defaultType,
                    dictionary_name: defaultType
                }];

                $scope.loadTypes = function(){
                    service.getByType(defaultType).then(function (data) {
                        var obj = angular.fromJson(data);
                        if (obj.result == 'success') {
                            $scope.dictionaryTypes = [{
                                dictionary_code: defaultType,
                                dictionary_name: defaultType
                            }].concat(obj.data.items);
                        }
                    });
                };

                $scope.ok = function () {
                    if(item.dictionary_id == ''){
                        service.addItem($scope.item).then(function(data){
                            $uibModalInstance.close(data);
                        });
                    }else{
                        service.updateItem($scope.item).then(function(data){
                            $uibModalInstance.close(data);
                        });
                    }
                };

                $scope.cancel = function () {
                    $uibModalInstance.dismiss('cancel');
                };

                $scope.loadTypes();
            }]);
    });